﻿namespace Groundwork.Extensions
{
	using System;
	
	/// <summary>
	/// Description of ThrowIf.
	/// </summary>
	public static class ThrowIf
	{




 	    /// <summary>
        /// Throws a ArgumentException if value is null;
        /// </summary>
        /// <param name="value">value to inspect</param>
        /// <param name="argument">value to inspect</param>
        public static void ThrowIfNull(this object value, string argument = "")
        {
            if (value == null)
            {
                throw new ArgumentException(argument);
            }
        }

        /// <summary>
        /// Throws a ArgumentNullException if the value is null
        /// </summary>
        /// <param name="value">value to be examined</param>
        /// <param name="message">message to be thrown with exception</param>
        /// <param name="context">calling context</param>
        public static void ThrowIfNullOrEmpty(this string value, string message = "", string context = "")
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("value", string.Format("context: {0}, message: {1}", context, message));
            }
        }

        /// <summary>
        /// Throws a ArgumentNullException if a null is found
        /// </summary>
        /// <param name="values">value to examine</param>
        /// <param name="message">message to add to exception</param>
        /// <param name="context">throw context</param>
        public static void ThrowIfNullOrEmpty(this string[] values, string message = "", string context = "")
        {
            if (values != null)
            {
                throw new ArgumentNullException("value", string.Format("context: {0}, message: {1}", context, message));
            }
        }
        
        /// <summary>
        /// Throws an ArgumentOutOfRangeException if the value is greater than the specified boundary. The upper boundary is not inclusive
        /// </summary>
        /// <param name="value">value to be examined</param>
        /// <param name="boundary">boundary length</param>
        public static void ThrowIfGreaterThan(this string value, int boundary)
        {
            if (value != null)
            {
                if (value.Length > boundary)
                {
                    throw new ArgumentOutOfRangeException("value", value.Length, string.Empty);
                }
            }
            else
            {
                throw new ArgumentNullException("value");
            }
        }

        /// <summary>
        /// Throws an ArgumentOutOfRangeException if the value is less than the specified boundary. The lower boundary is not inclusive
        /// </summary>
        /// <param name="value">value to be examined</param>
        /// <param name="boundary">boundary length</param>
        public static void ThrowIfLessThan(this string value, int boundary)
        {
            if (value != null)
            {
                if (value.Length < boundary)
                {
                    throw new ArgumentOutOfRangeException("value", value.Length, "value was less that the value range");
                }
            }
            else
            {
                throw new ArgumentNullException("value");
            }
        }

        /// <summary>
        /// Throws an ArgumentOutOfRangeException if the value is less than or greater than the lower or upper boundaries. The upper and lower boundaries are not inclusive.
        /// </summary>
        /// <param name="value">value to be examined</param>
        /// <param name="lowerBoundary">Lower Boundary Length</param>
        /// <param name="upperBoundary">Upper Boundary length</param>
        public static void ThrowIfNotInRange(this string value, int lowerBoundary, int upperBoundary)
        {
            if (value != null)
            {
                if (value.Length < lowerBoundary || value.Length > upperBoundary)
                {
                    throw new ArgumentOutOfRangeException("value", string.Format("Value was not between {0} and {1}", lowerBoundary, upperBoundary));
                }
            }
            else
            {
                throw new ArgumentNullException("value");
            }
        }
	}
}
